package com.beidu.lottery.call.service.portal.filter;

import com.beidu.lottery.call.common.BaseContextHandler;
import com.beidu.lottery.call.constant.ActivityTypeEnum;
import com.beidu.lottery.call.entity.PortalActivityRule;
import com.beidu.lottery.call.exception.ServiceException;
import com.beidu.lottery.call.service.portal.chain.AbstractChainHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 抽奖白名单校验
 **/
@Component
@Slf4j
public class LotteryWhiteListValidate implements AbstractChainHandler<PortalActivityRule> {

    @Override
    public void handler(PortalActivityRule rule) {
        log.info("LotteryWhiteListValidate start");
        // 不要求登录
        if (rule.getIsLogin() == 0) {
            return;
        }
        String whiteList = rule.getPhoneWhiteList();
        // 无白名单配置
        if (StringUtils.isEmpty(whiteList)) {
            return;
        }
        // 用户手机号
        String phone = BaseContextHandler.getPhone();
        // 无手机号 || 不在白名单内
        if (StringUtils.isEmpty(phone) || !whiteList.contains(phone)) {
            log.info("用户手机号 => {}, 白名单 => {}", phone, whiteList);
            throw new ServiceException("该活动仅限白名单内的用户参与");
        }
    }

    @Override
    public String mark() {
        return ActivityTypeEnum.LOTTERY.getType();
    }

    @Override
    public int getOrder() {
        return 20;
    }
}